package com.jinkaimen.kqb.ui.holder;

import android.view.View;
import android.widget.TextView;

import com.google.zxing.integration.android.IntentIntegrator;
import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.base.BaseViewHolder;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.bean.ShopBean;
import com.jinkaimen.kqb.ui.activity.addmerchant.QrCodeActivity;

import de.greenrobot.event.EventBus;

public class ShopManagerViewHolder extends BaseViewHolder<ShopBean> implements View.OnClickListener {

    private BaseActivity mContext;
    private TextView mNameView;
    private View mIsMasterView;
    private ShopBean mShopBean;
    private TextView mBindQrCodeView;

    public ShopManagerViewHolder(BaseActivity context) {
        super(R.layout.list_shop_manager_item);
        this.mContext = context;
    }

    @Override
    protected void initView() {
        mNameView = findViewById(R.id.tv_list_shop_manager_item_shop_name);
        mIsMasterView = findViewById(R.id.tv_list_shop_manager_item_shop_is_master);
        mBindQrCodeView = findViewById(R.id.tv_list_shop_manager_item_shop_bind_qrcode);
        mBindQrCodeView.setOnClickListener(this);
    }

    @Override
    protected void refreshView(ShopBean shopBean) {
        mShopBean = shopBean;
        mNameView.setText(mShopBean.name);
        mIsMasterView.setVisibility(mShopBean.type?View.VISIBLE:View.GONE);

        if (shopBean.countQR == 0){
            mBindQrCodeView.setText("绑定二维码");
        } else {
            mBindQrCodeView.setText("已绑定二维码");
        }
    }

    public void startScanQrcode(){
        //                假如你要用的是fragment进行界面的跳转
//                IntentIntegrator intentIntegrator = IntentIntegrator.forSupportFragment(ShopFragment.this)
//
        IntentIntegrator intentIntegrator = new IntentIntegrator(mContext);
        intentIntegrator
                .setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)
                .setPrompt("将二维码/条码放入框内，即可自动扫描")//写那句提示的话
                .setOrientationLocked(false)//扫描方向固定
                .setCaptureActivity(QrCodeActivity.class) // 设置自定义的activity是CustomActivity
                .initiateScan(); // 初始化扫描
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_list_shop_manager_item_shop_bind_qrcode:
                if (mShopBean.countQR == 0){
                    EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_START_BIND_QRCODE_SHOP_ID, mShopBean.id));
                    startScanQrcode();
                }
                break;
        }
    }
}